import { existsSync } from "fs";
import path from "path";

const moment = require('moment');




//创建游戏组件视图
export function createGameComponentView(dirPath:string,fileName:string,content:string,isECSComp:boolean = true):Promise<void>{
    return new Promise<void>(async (resolve,rejext)=>{
        //创建脚本
        let className  = fileName+"View";   
        let scriptUrl  = ""
        if (isECSComp){
            scriptUrl = path.join(dirPath,fileName)+'ViewComp.ts'
        }else{
            scriptUrl = path.join(dirPath,fileName)+'View.ts'
        }
        if (existsSync(scriptUrl)) {
            Editor.Dialog.error("文件已存在");
            return;
        }else{
            content = content.replace(/<%Name%>/g,className);
      
            const now  = moment().format('YYYY-MM-DD HH:mm:ss');

            content = content.replace(/<%date%>/g,now);
            await Editor.Message.request("asset-db",'create-asset',scriptUrl,content)
        }

        // //创建prefab
        let prefabUrl = path.join(dirPath,fileName+".prefab");
        if (!existsSync(prefabUrl)) {
            if (isECSComp) {
                className = className+"Comp";
            }
            await Editor.Message.request('scene','execute-scene-script',{
                name:'hiyooo-plugin-framework',
                method:'createPrefab',
                args:[fileName,className,prefabUrl]
            });
        }
        // 闪烁
        Editor.Message.send("assets",'twinkle',scriptUrl);
        // 打开
        Editor.Message.request('asset-db','open-asset',prefabUrl);
        // 打开脚本
        Editor.Message.request('asset-db','open-asset',scriptUrl);

        resolve()
           

    })
}

//创建游戏组件脚本
/**
 * 
*/
export function createGameComponentScript(dirPath:string,fileName:string,content:string):Promise<void>{
    return new Promise<void>(async (resolve,reject)=>{
        const className = fileName+"View";
        content =  content.replace(/<%Name%>/g,className);

        let scriptPath = path.join(dirPath,fileName+".ts");

        await Editor.Message.request("asset-db",'create-asset',scriptPath,content)

        resolve();
    });
}

//创建ECS模块脚本 和文件夹
export function createScriptModule(dirPath:string,fileName:string,content:string):Promise<void>{
    return new Promise<void>(async (resolve,reject)=>{
        const pathName  = fileName.toLowerCase();
        const pathModule = path.join(dirPath,pathName);
        //文件夹的创建
        if (!existsSync(pathModule)) {
            await Editor.Message.request('asset-db','create-asset',pathModule,null);
        }
        const subPathView = path.join(pathModule,"view");
        if (!existsSync(subPathView)) {
            await Editor.Message.request('asset-db','create-asset',subPathView,null);
        }

        const subBllPath = path.join(pathModule,"bll");
        if (!existsSync(subBllPath)) {
            await Editor.Message.request('asset-db','create-asset',subBllPath,null);
        }

        const subModelPath = path.join(pathModule,"model");
        if (!existsSync(subModelPath)) {
            await Editor.Message.request('asset-db','create-asset',subModelPath,null);
        }

        const scriptPath =`${path.join(pathModule,fileName)}.ts`
        if (!existsSync(scriptPath)) {

            content = content.replace(/<%Name%>/g,fileName);
            const now = moment().format('YYYY-MM-DD HH:mm:ss');
            content = content.replace(/<%date%>/g,now);
            await Editor.Message.request("asset-db",'create-asset',scriptPath,content)
        }   

        Editor.Message.send("assets",'twinkle',scriptPath);

        Editor.Message.request('asset-db','open-asset',scriptPath)

        resolve();

    });

    
}

//创建model层的脚本
export function createModelScript(dirPath:string,fileName:string,content:string,isECSComp:boolean = true):Promise<void>{
    return new Promise<void>(async(resolve,reject)=>{
        let scriptUrl = ""
        if (isECSComp) {
            scriptUrl = `${path.join(dirPath,fileName)}Comp.ts` 
        }else{
            scriptUrl = `${path.join(dirPath,fileName)}.ts`
        }

        if (!existsSync(scriptUrl)) {
            content = content.replace(/<%Name%>/g,fileName)
            const now = moment().format('YYYY-MM-DD HH:mm:ss');
            content = content.replace(/<%date%>/g,now);
            await Editor.Message.request("asset-db",'create-asset',scriptUrl,content)
        }

        Editor.Message.send("assets",'twinkle',scriptUrl);
        Editor.Message.request('asset-db','open-asset',scriptUrl)
        resolve();
    });

}


export function createBllScript(dirPath:string,fileName:string,content:string,moduleName:string):Promise<void>{
    return new Promise<void>(async(resolve,reject)=>{
        const scriptUrl = `${path.join(dirPath,fileName)}.ts`
        if (!existsSync(scriptUrl)) {
            content = content.replace(/<%Name%>/g,fileName);
            let now = moment().format('YYYY-MM-DD HH:mm:ss');   
            content = content.replace(/<%date%>/g,now);
            content  = content.replace(/<%ModuleName%>/g,moduleName);
            await Editor.Message.request("asset-db","create-asset",scriptUrl,content)
        }

        // 闪烁
        Editor.Message.send("assets",'twinkle',scriptUrl);
        Editor.Message.request('asset-db','open-asset',scriptUrl)
        resolve();
    })
}